package com.woniu.controller;


import com.alibaba.excel.EasyExcel;
import com.woniu.aspect.Log;
import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.QueryOrderInfoDTO;
import com.woniu.entity.vo.OrderInfoVo;
import com.woniu.pojo.OrderInfo;
import com.woniu.pojo.User;
import com.woniu.service.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/orderInfo")
@Slf4j
@Api(tags = "订单信息管理")
public class OrderInfoController extends BaseController {
    @Autowired
    private OrderInfoService orderInfoService;


    @GetMapping("/all")
    @ApiOperation("查询所有")
    public Result listAll(){
        List<OrderInfoVo> orders = orderInfoService.listInfo();
        return toDataResult(orders);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询")
    public Result findById(@PathVariable Integer id){
        OrderInfo OrderInfo = orderInfoService.getById(id);
        return toDataResult(OrderInfo);
    }

    @PostMapping("/searchPage/{currentPage}/{pageSize}")
    @ApiOperation("分页查询")
    public Result searchPage(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize,
                             @RequestBody QueryOrderInfoDTO queryOrderDTO){
        PageData pageData = orderInfoService.searchPage(currentPage,pageSize,queryOrderDTO);
        return toDataResult(pageData);
    }

    @Log(type = "导出")
    @ApiOperation("导出数据")
    @PostMapping("/export")
    public void listSearch(@RequestBody(required = false) QueryOrderInfoDTO queryOrderDTO,
                           HttpServletResponse response) throws IOException {
        PageData pageData =  orderInfoService.searchPage(1, 1000, queryOrderDTO);
        List<OrderInfo> orderInfos = (List<OrderInfo>) pageData.getData();
        EasyExcel.write(response.getOutputStream(), User.class).sheet().doWrite(orderInfos);
    }
}
